package com.KKd.controller;

import com.KKd.pojo.User;
import com.KKd.utils.JsonUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 14587
 * @date 2020/11/4 21:07
 */
//@Controller
@RestController
public class UserController {
    @RequestMapping("/j1")
    //@ResponseBody//使用这个注解就不会走视图解析器，直接返回字符串
    public String json1() throws JsonProcessingException {
        //jackson里有一个objectMapper类，可以把对象转换成字符串，以键值对的形式包装
        ObjectMapper mapper = new ObjectMapper();
        User user = new User(1, "dou", 18);
        String str = mapper.writeValueAsString(user);
        return str;
    }
    @RequestMapping("/j2")
    //@ResponseBody//使用这个注解就不会走视图解析器，直接返回字符串
    public String json2() throws JsonProcessingException {
        //jackson里有一个objectMapper类，可以把对象转换成字符串，以键值对的形式包装
        ObjectMapper mapper = new ObjectMapper();
        User user1 = new User(1, "dou", 18);
        User user2 = new User(1, "dou", 18);
        User user3 = new User(1, "dou", 18);
        User user4 = new User(1, "dou", 18);
        List<User> users = new ArrayList<User>();
        users.add(user1);
        users.add(user2);
        users.add(user3);
        users.add(user4);
        return JsonUtils.getJson(users);
    }

    @RequestMapping("/j3")
    //@ResponseBody//使用这个注解就不会走视图解析器，直接返回字符串
    public String json3() throws JsonProcessingException {
        Date date = new Date();
        return JsonUtils.getJson(date,"yyyy-MM-dd hh:mm:ss");
    }
}
